package edu.scau.mis.system.controller;

import edu.scau.mis.core.annotation.RequestLog;
import edu.scau.mis.core.enums.BusinessType;
import edu.scau.mis.system.domain.FileInfo;
import edu.scau.mis.system.domain.SysOss;
import edu.scau.mis.system.service.FileService;
import edu.scau.mis.system.service.ISysOssService;
import edu.scau.mis.core.domain.AjaxResult;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;
/**
 * (SysOss)表控制层
 *
 * @author CJW
 * @since 2024-05-11 19:23:06
 */
@RestController
@RequestMapping("sysOss")
public class SysOssController {
    /**
     * 服务对象
     */
    @Autowired
    private ISysOssService sysOssService;

    @Autowired
    private FileService fileService;



    /**
     * 以流的形式上传图片
     *
     * @param file
     * @return 返回访问路径
     */
    @RequestLog(title = "上传",businessType = BusinessType.OTHER)
    @PostMapping("upload")
    public AjaxResult uploadFile(@RequestParam(value = "file") MultipartFile file, @RequestParam("productId")Long productId , @RequestParam("title")String title)  {
        FileInfo fileInfo = fileService.uploadFile(file);

        String storage = fileInfo.getStorage();
        String bucket = fileInfo.getBucketName();
        String fileName = fileInfo.getFileName();
        String fileOriginalName = fileInfo.getFileOriginalName();
        Long fileSize = fileInfo.getFileSize();
        String contentType = fileInfo.getContentType();
        String url = fileInfo.getFileUrl();

        System.out.println("提供商是：" + storage);
        System.out.println("存储桶是：" + bucket);
        System.out.println("文件名称是：" + fileName);
        System.out.println("文件原名称是：" + fileOriginalName);
        System.out.println("文件大小是：" + fileSize);
        System.out.println("文件类型是：" + contentType);
        System.out.println("文件url是：" + url);
        System.out.println("文件的标题是：" + title);
        System.out.println("文件的产品id是：" + productId);

        SysOss sysOss = new SysOss();
        sysOss.setStorage(storage);
        sysOss.setBucket(bucket);
        sysOss.setFileName(fileName);
        sysOss.setFileOriginalName(fileOriginalName);
        sysOss.setFileSize(fileSize);
        sysOss.setContentType(contentType);
        sysOss.setFileUrl(url);
        sysOss.setTitle(title);
        sysOss.setProductId(productId);
        sysOss.setCreateTime(new Date());

        sysOssService.insert(sysOss);

        return AjaxResult.success(fileInfo);
    }

    /**
     * 查询数据列表
     *
     * @param sysOss 筛选条件
     * @return 查询结果
     */
    @RequestLog(title = "查询数据列表",businessType = BusinessType.GET)
    @GetMapping("/list")
    @ApiOperation("查询数据列表")
    public AjaxResult list(SysOss sysOss) {
        return AjaxResult.success(this.sysOssService.selectList(sysOss));
    }

    /**
     * 分页查询数据列表
     *
     * @param sysOss 筛选条件
     * @return 查询结果
     */
    @RequestLog(title = "分页查询数据",businessType = BusinessType.GET)
    @GetMapping("/page")
    @ApiOperation("分页查询数据列表")
    public AjaxResult page(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize, SysOss sysOss) {
        PageHelper.startPage(pageNum,pageSize);
        List<SysOss> list = this.sysOssService.selectList(sysOss);
        PageInfo<List> pageInfo = new PageInfo(list);
        return AjaxResult.success(pageInfo);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @RequestLog(title = "通过主键查询单条数据",businessType = BusinessType.GET)
    @GetMapping("{id}")
    @ApiOperation("通过主键查询单条数据")
    public AjaxResult getById(@PathVariable("id") Long id) {
        return AjaxResult.success(this.sysOssService.selectById(id));
    }

    /**
     * 新增数据
     *
     * @param sysOss 实体
     * @return 新增结果
     */
    @PostMapping
    @RequestLog(title = "通过主键查询单条数据",businessType = BusinessType.INSERT)
    @ApiOperation("新增数据")
    public AjaxResult add(@RequestBody SysOss sysOss) {
        return AjaxResult.success(this.sysOssService.insert(sysOss));
    }

    /**
     * 编辑数据
     *
     * @param sysOss 实体
     * @return 编辑结果
     */
    @PutMapping
    @RequestLog(title = "编辑数据",businessType = BusinessType.UPDATE)
    @ApiOperation("编辑数据")
    public AjaxResult edit(@RequestBody SysOss sysOss) {
        return AjaxResult.success(this.sysOssService.update(sysOss));
    }

    /**
     * 批量删除数据
     *
     * @param ids 主键
     * @return 删除是否成功
     */
    @DeleteMapping("/{ids}")
    @RequestLog(title = "批量删除数据",businessType = BusinessType.DELETE)
    @ApiOperation("批量删除数据")
    public AjaxResult deleteById(@PathVariable Long ids) {
        return AjaxResult.success(this.sysOssService.deleteByIds(ids));
    }
}
